package mrusanov.calendar

import java.util.{Date, Calendar}


class SeasonCalendar(year:Int) {
  private val worldCupMatchesCount = 13

  val seasonFinishWeek = 50
  val seasonStartWeek = 8
  val nationalChampionshipsDayOfWeek = Calendar.SATURDAY
  val nationalCupsDayOfWeek = Calendar.THURSDAY
  val worldCupsDayOfWeek = Calendar.TUESDAY

  def datesOfWorldCup = chooseWeeksForMatches(worldCupMatchesCount).map(weekToDate(worldCupsDayOfWeek)(_))
  def datesOfLeagueTournament(rounds:Int) = chooseWeeksForMatches(rounds).map(weekToDate(nationalChampionshipsDayOfWeek)(_))
  def datesOfNationalCups(rounds:Int) = chooseWeeksForMatches(rounds).map(weekToDate(nationalCupsDayOfWeek)(_))

  private def weekToDate(dayOfWeek:Int)(week:Int):Date = {
    val calendar = Calendar.getInstance()
    calendar.clear()
    calendar.set(Calendar.YEAR, year)
    calendar.set(Calendar.WEEK_OF_YEAR, week)
    calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek)
    calendar.getTime
  }

  private def chooseWeeksForMatches(matchesCount:Int):Vector[Int] = {
    val distanceBetweenMatches = (seasonFinishWeek - seasonStartWeek).toDouble / (matchesCount + 1)
    Vector() ++ 0.until(matchesCount).map(n => seasonStartWeek + (1 + n) * distanceBetweenMatches).map(_.round.toInt)
  }
}


